﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Autodesk.Revit.DB;
using DataBase;

namespace YJKDataLookAfter
{
	public class DbBeam : DbObject
	{
		[Category("属性")]
		[Description("截面")]
		[DisplayName("截面")]
		[ReadOnly(true)]
		public SectionParameter SectionParameter
		{
			get
			{
				return this.mParameter;
			}
			set
			{
				this.mParameter = value;
			}
		}

		[Category("属性")]
		[Description("起点")]
		[DisplayName("起点")]
		[ReadOnly(true)]
		public XYZ StartPt
		{
			get
			{
				return this.mStartPt;
			}
			set
			{
				this.mStartPt = value;
			}
		}

		[Category("属性")]
		[Description("终点")]
		[DisplayName("终点")]
		[ReadOnly(true)]
		public XYZ EndPt
		{
			get
			{
				return this.mEndPt;
			}
			set
			{
				this.mEndPt = value;
			}
		}

		[Category("属性")]
		[Description("凸度")]
		[DisplayName("凸度")]
		[ReadOnly(true)]
		public double Bulge
		{
			get
			{
				return this.mBulge;
			}
			set
			{
				this.mBulge = value;
			}
		}

		[Category("属性")]
		[Description("建筑模型Id")]
		[DisplayName("建筑模型Id")]
		[ReadOnly(true)]
		public int RacId
		{
			get
			{
				return this.mRacId;
			}
			set
			{
				this.mRacId = value;
			}
		}

		[Category("属性")]
		[Description("结构模型Id")]
		[DisplayName("结构模型Id")]
		[ReadOnly(true)]
		public int RstId
		{
			get
			{
				return this.mRstId;
			}
			set
			{
				this.mRstId = value;
			}
		}

		[Category("属性")]
		[Description("建筑模型路径")]
		[DisplayName("建筑模型路径")]
		[ReadOnly(true)]
		public string ArchPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, this.mDatabase).RacFilePath;
			}
		}

		[Category("属性")]
		[Description("结构模型路径")]
		[DisplayName("结构模型路径")]
		[ReadOnly(true)]
		public string RstPath
		{
			get
			{
				return DbProject.GetFromDB(this.mProjectId, this.mDatabase).RstFilePath;
			}
		}

		public DbBeam(int projectId, int racId, int rstId, XYZ startPt, XYZ endPt, double bulge, SectionParameter sParam)
		{
			this.mProjectId = projectId;
			this.mStartPt = startPt;
			this.mEndPt = endPt;
			this.mBulge = bulge;
			this.mParameter = sParam;
			this.mRacId = racId;
			this.mRstId = rstId;
		}

		public static void Clear(DbProject project)
		{
			foreach (DbBeam dbBeam in DbBeam.GetByProject(project))
			{
				dbBeam.RemoveFromDb();
			}
		}

		public static List<DbBeam> GetByProject(DbProject project)
		{
			List<DbBeam> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbBeam> list = new List<DbBeam>();
				string sql = string.Format("SELECT Id,ProjectId,RacID,RstID,StartPtX,StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge,SectionType,Parameter1,Parameter2 FROM tbbeam WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					result = list;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacID"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstID"]);
						XYZ startPt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["StartPtX"]), Convert.ToDouble(dataTable.Rows[i]["StartPtY"]), Convert.ToDouble(dataTable.Rows[i]["StartPtZ"]));
						XYZ endPt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["EndPtX"]), Convert.ToDouble(dataTable.Rows[i]["EndPtY"]), Convert.ToDouble(dataTable.Rows[i]["EndPtZ"]));
						double bulge = Convert.ToDouble(dataTable.Rows[i]["Bulge"]);
						SectionType sectionType = (SectionType)Convert.ToInt32(dataTable.Rows[i]["SectionType"]);
						SectionParameter sParam = null;
						if (sectionType == SectionType.Arc)
						{
							sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[i]["Parameter1"]));
						}
						else if (sectionType == SectionType.Rect)
						{
							double param = Convert.ToDouble(dataTable.Rows[i]["Parameter1"]);
							double param2 = Convert.ToDouble(dataTable.Rows[i]["Parameter2"]);
							sParam = new SectionParameter(param, param2);
						}
						list.Add(new DbBeam(projectId, racId, rstId, startPt, endPt, bulge, sParam)
						{
							mId = mId,
							mDatabase = project.Database
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbBeam GetFromDB(int id, IDatabase db)
		{
			DbBeam result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT ProjectId,RacID,RstID,StartPtX,StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge,SectionType,Parameter1,Parameter2 FROM tbbeam WHERE ID={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					int projectId = Convert.ToInt32(dataTable.Rows[0]["ProjectId"]);
					int racId = Convert.ToInt32(dataTable.Rows[0]["RacID"]);
					int rstId = Convert.ToInt32(dataTable.Rows[0]["RstID"]);
					XYZ startPt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["StartPtX"]), Convert.ToDouble(dataTable.Rows[0]["StartPtY"]), Convert.ToDouble(dataTable.Rows[0]["StartPtZ"]));
					XYZ endPt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["EndPtX"]), Convert.ToDouble(dataTable.Rows[0]["EndPtY"]), Convert.ToDouble(dataTable.Rows[0]["EndPtZ"]));
					double bulge = Convert.ToDouble(dataTable.Rows[0]["Bulge"]);
					SectionType sectionType = (SectionType)Convert.ToInt32(dataTable.Rows[0]["SectionType"]);
					SectionParameter sParam = null;
					if (sectionType == SectionType.Arc)
					{
						sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[0]["Parameter1"]));
					}
					else if (sectionType == SectionType.Rect)
					{
						double param = Convert.ToDouble(dataTable.Rows[0]["Parameter1"]);
						double param2 = Convert.ToDouble(dataTable.Rows[0]["Parameter2"]);
						sParam = new SectionParameter(param, param2);
					}
					result = new DbBeam(projectId, racId, rstId, startPt, endPt, bulge, sParam)
					{
						mId = id,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbBeam> GetFromDB(IDatabase db)
		{
			List<DbBeam> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbBeam> list = new List<DbBeam>();
				string sql = string.Format("SELECT Id,ProjectId,RacID,RstID,StartPtX,StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge,SectionType,Parameter1,Parameter2 FROM tbbeam", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int racId = Convert.ToInt32(dataTable.Rows[i]["RacID"]);
						int rstId = Convert.ToInt32(dataTable.Rows[i]["RstID"]);
						XYZ startPt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["StartPtX"]), Convert.ToDouble(dataTable.Rows[i]["StartPtY"]), Convert.ToDouble(dataTable.Rows[i]["StartPtZ"]));
						XYZ endPt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["EndPtX"]), Convert.ToDouble(dataTable.Rows[i]["EndPtY"]), Convert.ToDouble(dataTable.Rows[i]["EndPtZ"]));
						double bulge = Convert.ToDouble(dataTable.Rows[i]["Bulge"]);
						SectionType sectionType = (SectionType)Convert.ToInt32(dataTable.Rows[i]["SectionType"]);
						SectionParameter sParam = null;
						if (sectionType == SectionType.Arc)
						{
							sParam = new SectionParameter(Convert.ToDouble(dataTable.Rows[i]["Parameter1"]));
						}
						else if (sectionType == SectionType.Rect)
						{
							double param = Convert.ToDouble(dataTable.Rows[i]["Parameter1"]);
							double param2 = Convert.ToDouble(dataTable.Rows[i]["Parameter2"]);
							sParam = new SectionParameter(param, param2);
						}
						list.Add(new DbBeam(projectId, racId, rstId, startPt, endPt, bulge, sParam)
						{
							mId = mId,
							mDatabase = db
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbbeam (SectionType, Parameter1,Parameter2,RacID,RstID,StartPtX,StartPtY,StartPtZ,EndPtX,EndPtY,EndPtZ,Bulge,ProjectId) VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12})", new object[]
				{
					(int)this.mParameter.SectionType,
					this.mParameter.Parameter1,
					this.mParameter.Parameter2,
					this.mRacId,
					this.mRstId,
					this.mStartPt.X,
					this.mStartPt.Y,
					this.mStartPt.Z,
					this.mEndPt.X,
					this.mEndPt.Y,
					this.mEndPt.Z,
					this.mBulge,
					this.mProjectId
				});
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbbeam", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbbeam SET SectionType = {0},Parameter1={1},Parameter2={2},RacID={3},RstID={4},StartPtX={5},StartPtY={6},StartPtZ={7},EndPtX={8},EndPtY={9},EndPtZ={10},Bulge={11},ProjectId={12} WHERE Id ={13}", new object[]
				{
					this.mParameter.SectionType,
					this.mParameter.Parameter1,
					this.mParameter.Parameter2,
					this.mRacId,
					this.mRstId,
					this.mStartPt.X,
					this.mStartPt.Y,
					this.mStartPt.Z,
					this.mEndPt.X,
					this.mEndPt.Y,
					this.mEndPt.Z,
					this.mBulge,
					this.mProjectId,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbbeam WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override string ToString()
		{
			return string.Format("结构({0})-建筑({1})", this.mRstId.ToString(), this.mRacId.ToString());
		}

		private int mProjectId;

		private SectionParameter mParameter;

		private XYZ mStartPt;

		private XYZ mEndPt;

		private double mBulge;

		private int mRacId;

		private int mRstId;
	}
}
